<?php

/**
 * Description of Auth
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: Auth.php 29 2009-05-14 15:35:03Z monzee $
 */
class Admin_Model_Auth
{
    /**
     * Specify a custom hash callback. This Callback should return a string.
     * 
     * @var callback
     */
    static public $hash;
    /**
     * Invoke hash callback. Any args will be passed to the callback.
     *
     * If the callback is invalid, returns the md5 hash of $password.
     *
     * Sample usage:
     *
     * Admin_Model_Auth::$hash = function ($password, $salt) {
     *     return md5($salt . $password);
     * };
     * $password = Admin_Model_Auth::hash('secret', 'tris-acetate-EDTA');
     *
     * @param string $password
     * @return string
     */
    static public function hash($password)
    {
        if (null !== self::$hash && is_callable($hash)) {
            $args = func_get_args();
            return call_user_func_array(self::$hash, $args);
        }
        return md5($password);
    }

    /**
     *
     * @var Zend_Auth_Adapter_Interface
     */
    static protected $_adapter;
    /**
     * Factory method.
     *
     * Arguments will be passed to the adapter constructor. See adapter classes
     * to see what the required args are, but typically they require the
     * username and password.
     *
     * @return Zend_Auth_Adapter_Interface
     */
    static public function getAdapter()
    {
        if (null === self::$_adapter) {
            $config = Admin_Bootstrap::getConfig('auth');

            $adapter = $config->get('adapter', 'Array');
            $prefix = rtrim($config->get('prefix', 'Admin_Model_Auth'), '_');
            $adapterClass = $prefix . '_' . ucfirst($adapter);

            $args = func_get_args();
            self::$_adapter = call_user_func_array(
                    array(new ReflectionClass($adapterClass), 'newInstance'),
                    $args
                );
        }

        return self::$_adapter;
    }

}